#!/bin/bash

# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Script to set /etc/lsb-release on the root file system. This script is run by
# build_image inside chroot.

readonly COMMON_SH=/usr/lib/crosutils/common.sh
if [ ! -r "${COMMON_SH}" ]; then
  echo "ERROR: Run inside chroot."
  exit 1
fi

. "${COMMON_SH}"

# Flags
DEFINE_string board "" "The board to build an image for."
DEFINE_string root "" "The root file system to write /etc/lsb-release to."

# Parse command line
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"

set -e

ROOT_FS_DIR="$FLAGS_root"
[ -n "$ROOT_FS_DIR" ] || die "--root is required."
[ -d "$ROOT_FS_DIR" ] || die "Root FS does not exist? ($ROOT_FS_DIR)"
[ -n "${CHROMEOS_VERSION_STRING}" ] || die "chromeos_version.sh isn't sourced."

hostname=$(hostname --fqdn || echo ${HOSTNAME:-localhost.localdomain})

CHROMEOS_VERSION_NAME="Chromium OS"
CHROMEOS_VERSION_AUSERVER=\
${CHROMEOS_VERSION_AUSERVER:-"http://${hostname}:8080/update"}
CHROMEOS_VERSION_DEVSERVER=\
${CHROMEOS_VERSION_DEVSERVER:-"http://${hostname}:8080"}

# Official builds must set CHROMEOS_OFFICIAL=1.
if [ ${CHROMEOS_OFFICIAL:-0} = 1 ]; then
  # Official builds (i.e., buildbot)
  CHROMEOS_VERSION_TRACK="dev-channel"
  CHROMEOS_VERSION_NAME="Chrome OS"
  CHROMEOS_BUILD_TYPE="Official Build"
  CHROMEOS_VERSION_DESCRIPTION="${CHROMEOS_VERSION_STRING} \
(${CHROMEOS_BUILD_TYPE}) ${CHROMEOS_VERSION_TRACK} $FLAGS_board test"
  CHROMEOS_VERSION_AUSERVER="https://tools.google.com/service/update2"
  CHROMEOS_VERSION_DEVSERVER=""
elif [ "$USER" = "chrome-bot" ]; then
  # Continuous builder
  CHROMEOS_VERSION_TRACK="buildbot-build"
  CHROMEOS_BUILD_TYPE="Continuous Builder - Builder: ${BUILDBOT_BUILD:-"N/A"}"
  CHROMEOS_VERSION_DESCRIPTION="${CHROMEOS_VERSION_STRING} \
(${CHROMEOS_BUILD_TYPE}) $FLAGS_board"
else
  # Developer hand-builds
  CHROMEOS_VERSION_TRACK=${CHROMEOS_VERSION_TRACK:-"developer-build"}
  CHROMEOS_BUILD_TYPE="Developer Build - $USER"
  CHROMEOS_VERSION_DESCRIPTION="${CHROMEOS_VERSION_STRING} \
(${CHROMEOS_BUILD_TYPE}) ${CHROMEOS_VERSION_TRACK} $FLAGS_board"
fi

# Set google-specific version numbers:
# CHROMEOS_RELEASE_BOARD is the target board identifier.
# CHROMEOS_RELEASE_BRANCH_NUMBER is the Chrome OS branch number
# CHROMEOS_RELEASE_BUILD_NUMBER is the Chrome OS build number
# CHROMEOS_RELEASE_BUILD_TYPE is the type of build (official, from developers,
# etc..)
# CHROMEOS_RELEASE_CHROME_MILESTONE is the Chrome milestone (also named Chrome
#   branch).
# CHROMEOS_RELEASE_DESCRIPTION is the version displayed by Chrome; see
#   chrome/browser/chromeos/chromeos_version_loader.cc.
# CHROMEOS_RELEASE_NAME is a human readable name for the build.
# CHROMEOS_RELEASE_PATCH_NUMBER is the patch number for the current branch.
# CHROMEOS_RELEASE_TRACK and CHROMEOS_RELEASE_VERSION are used by the software
#   update service.
# TODO(skrul):  Remove GOOGLE_RELEASE once Chromium is updated to look at
#   CHROMEOS_RELEASE_VERSION for UserAgent data.
sudo_append "${ROOT_FS_DIR}/etc/lsb-release" <<EOF
CHROMEOS_RELEASE_BOARD=$FLAGS_board
CHROMEOS_RELEASE_BRANCH_NUMBER=$CHROMEOS_BRANCH
CHROMEOS_RELEASE_BUILD_NUMBER=$CHROMEOS_BUILD
CHROMEOS_RELEASE_BUILD_TYPE=$CHROMEOS_BUILD_TYPE
CHROMEOS_RELEASE_CHROME_MILESTONE=$CHROME_BRANCH
CHROMEOS_RELEASE_DESCRIPTION=$CHROMEOS_VERSION_DESCRIPTION
CHROMEOS_RELEASE_NAME=$CHROMEOS_VERSION_NAME
CHROMEOS_RELEASE_PATCH_NUMBER=$CHROMEOS_PATCH
CHROMEOS_RELEASE_TRACK=$CHROMEOS_VERSION_TRACK
CHROMEOS_RELEASE_VERSION=$CHROMEOS_VERSION_STRING
GOOGLE_RELEASE=$CHROMEOS_VERSION_STRING
CHROMEOS_AUSERVER=$CHROMEOS_VERSION_AUSERVER
CHROMEOS_DEVSERVER=$CHROMEOS_VERSION_DEVSERVER
EOF
